import socket
"""
使用tcp socket来实现网站服务器
"""

def handle_request(client):
    buf = client.recv(1024).decode()
    # print(buf)
    # print(buf)
    # for s in str(buf).split('\r\n'):
    #     print(s)
    print("a request comes:")
    msg = "HTTP/1.1 200 OK\r\n\r\n"  # HTTP头信息
    client.send( msg.encode())
    msg = "<h1 >Hello, World! </h1>"
    client.send(msg.encode())


def main():
    ip_port = ("localhost", 8000)
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(ip_port)
    sock.listen(5)
    print("server begin: ")

    while True:
        conn, addr = sock.accept()
        handle_request(conn)
        conn.close()


if __name__ == "__main__":
    main()